package com.helloworld.demo.map;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class HelloConcurrentHashMap {
    public static void  main(String[] args) {
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();

        concurrentHashMap.put("1","2");

        concurrentHashMap.putIfAbsent("1","2");


        final ReentrantLock lock = new ReentrantLock();

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try{
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    while (!lock.tryLock()){
                        System.out.println("我在做别的事情");
                    }
                    System.out.println(Thread.currentThread().getName() + "获取到了锁");
                }finally {
                    lock.unlock();
                }
            }
        },"Thread-2").start();
    }
}
